﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DataLayer;

public partial class tab_addshare : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      if (!ActionValidator.ChkValid(ActionValidator.ActionTypeEnum.FailAction))
         Response.Redirect("/support/hacking.html");

      bool fail = false;
      Guid ID = Guid.Empty;
      try
      {
         ID = new Guid(Request.QueryString.ToString());
      }
      catch
      {
         ID = Guid.Empty;
      }

      if (ID != Guid.Empty)
      {
         using (var db = DatabaseHelper.GetDashboardData())
         {
            var TSAs = from p in db.Tab_ShareActivations
                       where p.ID == ID
                       select p;

            if (TSAs.Count() > 0)
            {
               var TSA = TSAs.First();
               var TSed = new Tab_ShareActivated();
               TSed.AccessLevel = TSA.AccessLevel;
               TSed.ActivateDate = DateTime.Now;
               TSed.Email = TSA.Email;
               TSed.EmailContent = TSA.EmailContent;
               TSed.EmailDate = TSA.EmailDate;
               TSed.TabID = TSA.TabID;

               db.Tab_ShareActivateds.InsertOnSubmit(TSed);
               db.Tab_ShareActivations.DeleteOnSubmit(TSA);

               var oUT = new UserTab();
               oUT.AccessLevel = TSA.AccessLevel;
               oUT.TabId = TSA.TabID;
               oUT.UserID = Functions.GetUserProviderKey();
               oUT.RoleID = Constants.Role_Individual;
               db.UserTabs.InsertOnSubmit(oUT);
               db.SubmitChanges();
            }
            else
            {
               fail = true;
            }
         }
      }
      else
      {
         fail = true;  
      }

      if (fail)
      {
         ActionValidator.IsValid(ActionValidator.ActionTypeEnum.FailAction);
         Response.Redirect(FormsAuthentication.DefaultUrl);
      }
   }
}
